package cn.itcast.bos.service.base;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.ICourierRepository;
import cn.itcast.bos.domain.base.Courier;
@Service
@Transactional
public class CourierServiceImpl implements CourierService {
	
	@Autowired
	private ICourierRepository courierRepository;
	
	@Override
	@RequiresPermissions("courier:add")
	public void save(Courier courier) {
		courierRepository.save(courier);

	}

	@Override
	public Page<Courier> findByPage(Specification<Courier> spec,Pageable pageable) {
		return courierRepository.findAll(spec,pageable);
	}

	@Override
	public void delMatch(String[] str) {
		for (String idStr : str) {
			int id = Integer.parseInt(idStr);
			courierRepository.updateDelTag(id);
		}
	}

	@Override
	public List<Courier> findNoAssociation() {
		Specification<Courier> specification= new Specification<Courier>() {
			
			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return predicate;
			}
		};
		return courierRepository.findAll(specification);
	}

	@Override
	public Courier findByCourierNum(String courierNum) {
		// TODO Auto-generated method stub
		return courierRepository.findByCourierNum(courierNum);
	}

}
